import type { LoginResult, ProfileParams } from '@/types/member'
import { defineStore } from 'pinia'
import { ref } from 'vue'

// 定义 Store
export const useMemberStore = defineStore(
  'member',
  () => {
    // 会员信息
    const profile = ref<LoginResult>()
    const userInfo = ref<ProfileParams>()

    // 保存会员信息，登录时使用
    const setProfile = (val: LoginResult) => {
      profile.value = val
    }
    // 保存会员信息，修改展示时使用
    const setUserInfo = (val: ProfileParams) => {
      userInfo.value = val
    }

    // 清理会员信息，退出时使用
    const clearProfile = () => {
      profile.value = undefined
    }

    // 记得 return
    return {
      profile,
      setProfile,
      clearProfile,
      setUserInfo,
      userInfo,
    }
  },
  // TODO: 持久化
  {
    persist: {
      storage: {
        getItem: (key: string) => {
          return uni.getStorageSync(key) || {}
        },
        setItem: (key: string, val: any) => {
          uni.setStorageSync(key, val)
        },
      },
    },
  },
)
